package ${project.packageName}.${project.moduleName}.interfaces.api;

import ${project.packageName}.${project.moduleName}.application.service.${tableStructure.classNameUppercase}AppService;
import ${project.packageName}.${project.moduleName}.domain.music.${tableStructure.classNameUppercase}Entity;
import ${project.packageName}.${project.moduleName}.domain.music.vo.${tableStructure.classNameUppercase}QueryVO;
import ${project.packageName}.${project.moduleName}.interfaces.converter.I${tableStructure.classNameUppercase}Converter;
import ${project.packageName}.${project.moduleName}.interfaces.dto.${tableStructure.classNameUppercase}PageDTO;
import ${project.packageName}.${project.moduleName}.interfaces.dto.${tableStructure.classNameUppercase}UploadDTO;
import ${project.packageName}.${project.moduleName}.interfaces.param.${tableStructure.classNameUppercase}PageParam;
import ${project.packageName}.${project.moduleName}.interfaces.param.${tableStructure.classNameUppercase}SaveParam;
import com.rabbitpre.tubit.common.ids.SnowflakeIdFactory;
import com.rabbitpre.tubit.core.page.PageData;
import com.rabbitpre.tubit.core.page.PageParam;
import com.rabbitpre.tubit.operatelog.core.annotations.OperateLog;
import com.rabbitpre.tubit.operatelog.core.enums.OperateTypeEnum;
import com.rabbitpre.tubit.web.identity.IdentityContext;
import com.rabbitpre.tubit.web.identity.IdentityContextHandler;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * ${tableStructure.tableComment}接口
 *
 * @author ${developer.author}
 * @date ${developer.datetime}
 */
@Api(tags = "${tableStructure.tableComment}接口")
@Validated
@RestController
@RequestMapping("/api/${tableStructure.className}")
public class ${tableStructure.classNameUppercase}Controller {

    private static final Logger logger = LoggerFactory.getLogger(${project.packageName}.${project.moduleName}.interfaces.api.${tableStructure.classNameUppercase}Controller.class);


    @Resource
    private ${tableStructure.classNameUppercase}AppService ${tableStructure.className}AppService;
    @Resource
    private SnowflakeIdFactory snowflakeIdFactory;

    /**
     * 添加${tableStructure.tableComment}
     *
     * @param ${tableStructure.className}SaveParam ${tableStructure.tableComment}命令
     * @return Boolean
     */
    @ApiOperation("添加${tableStructure.tableComment}")
    @PostMapping("/save")
    @OperateLog(operateType = OperateTypeEnum.CREATE)
    public Boolean save(@RequestBody @Validated ${tableStructure.classNameUppercase}SaveParam ${tableStructure.className}SaveParam) {
        ${tableStructure.classNameUppercase}Entity ${tableStructure.className}Entity = I${tableStructure.classNameUppercase}Converter.IMPL.commandToDo(${tableStructure.className}SaveParam);
        //通过身份上下文，获取身份信息
        IdentityContext identityContext = IdentityContextHandler.get();
        ${tableStructure.className}Entity.setTenantId(identityContext.getTenantId());
        //todo 部门id目前暂未进入上下文，先用-1代替，表示无部门
        ${tableStructure.className}Entity.setDeptId(-1L);
        ${tableStructure.className}Entity.setUserId(identityContext.getUid());
        ${tableStructure.className}Entity.setId(snowflakeIdFactory.nextId());
        return ${tableStructure.className}AppService.add${tableStructure.classNameUppercase}(${tableStructure.className}Entity);
    }

    /**
     * 根据主键id删除${tableStructure.tableComment}
     *
     * @param ${tableStructure.className}Id ${tableStructure.tableComment}ID
     * @return boolean
     */
    @ApiOperation("删除${tableStructure.tableComment}")
    @PostMapping("/delete")
    @OperateLog(operateType = OperateTypeEnum.DELETE)
    public Boolean deleteByPrimaryKey(@NotNull(message = "${tableStructure.className}Id 不能为空") Long ${tableStructure.className}Id) {
        return ${tableStructure.className}AppService.delete${tableStructure.classNameUppercase}ById(${tableStructure.className}Id);
    }

    /**
     * 根据条件分页查询${tableStructure.tableComment}
     *
     * @param ${tableStructure.className}PageParam ${tableStructure.tableComment}分页条件
     * @return PageData
     */
    @ApiOperation("根据条件分页查询${tableStructure.tableComment}")
    @PostMapping("/list")
    @OperateLog(operateType = OperateTypeEnum.QUERY)
    public PageData queryPageList(@RequestBody @Validated ${tableStructure.classNameUppercase}PageParam ${tableStructure.className}PageParam) {
        PageParam pageParam = PageParam.create(${tableStructure.className}PageParam);
        ${tableStructure.classNameUppercase}QueryVO ${tableStructure.className}QueryVO = I${tableStructure.classNameUppercase}Converter.IMPL.commandToVo(${tableStructure.className}PageParam);
        PageData<${tableStructure.classNameUppercase}Entity> listPageData = ${tableStructure.className}AppService.queryPageList(${tableStructure.className}QueryVO, pageParam);
        //执行查询
        List<${tableStructure.classNameUppercase}PageDTO> userDTOList = I${tableStructure.classNameUppercase}Converter.IMPL.doListToDtoList(listPageData.getData());
        //转换结果返回
        PageData<${tableStructure.classNameUppercase}PageDTO> pageData = new PageData<${tableStructure.classNameUppercase}PageDTO>().setData(userDTOList).setTotal(listPageData.getTotal());
        return pageData;
    }


}
